package com.kaiyidiannao.activity.user_detail;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.kaiyidiannao.activity.login_or_registered.LoginOrRegistered;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.common.util.AppUtils;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.data.Validate;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.UserBean;
import com.kaiyidiannao.model.req.model.GetEditPassword;
import com.kaiyidiannao.model.req.model.GetRePassword;
import com.kaiyidiannao.view.ActionBarView;
import com.taplinker.core.util.LogUtil;

import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by wzg on 14-4-16.
 */
public class EditPassword extends BaseActionBarActivity implements Observer {

    private EditText oldPassword;
    private EditText newPassword;
    private EditText newPasswordAgain;
    private ImageButton determine;
    private LinearLayout oldPasswordLinearLayout;

    private List<UserBean> userBeans = DataCenter.userLogin;

    private String oldpassword;
    private String newpassword;
    private String repassword;
    private SharedPreferences sharedPreferences;

    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_password);
        AppUtils.setupUI(findViewById(R.id.edit_psd), this);
        // 添加观察者
        DataCenter.getInstance().addObserver(this);
        init();
        findActionBar();
    }

    public void init() {
        oldPasswordLinearLayout = (LinearLayout) findViewById(R.id.oldPasswordLinearLayout);
        Intent intent = getIntent();
        final String flag = intent.getStringExtra("flag");
        final String mobile = intent.getStringExtra("mobile");
        if (flag != null) {
            if (flag.equals("flag")) {
                oldPasswordLinearLayout.setVisibility(View.GONE);
            }
        }

        oldPassword = (EditText) findViewById(R.id.oldPassword);
        newPassword = (EditText) findViewById(R.id.newPassword);
        newPasswordAgain = (EditText) findViewById(R.id.newPasswordAgain);
        determine = (ImageButton) findViewById(R.id.determine);
//        确认修改密码事件
        determine.setOnClickListener(new View.OnClickListener() {
                                         @Override
                                         public void onClick(View v) {
                                             oldpassword = oldPassword.getText().toString();
                                             newpassword = newPassword.getText().toString();
                                             repassword = newPasswordAgain.getText().toString();
                                             if (flag != null) {
                                                 if (flag.equals("flag")) {
                                                     if (!"".equals(newpassword) || newpassword != null) {
                                                         if (newpassword.length() >= 6 && newpassword.length() <= 20) {
                                                             if (newPassword.getText().toString().matches(Validate.passwordValidate) && newPasswordAgain.getText().toString().matches(Validate.passwordValidate)) {
                                                                 if (!"".equals(repassword) || repassword != null) {
                                                                     if (newpassword.equals(repassword)) {
                                                                         showProgressDialog("正在提交");
                                                                         new GetRePassword("repwd", mobile, newpassword, "1").execute();
                                                                         index = 1;
                                                                     } else {
                                                                         AlertDialog builder = new AlertDialog.Builder(EditPassword.this).setTitle("错误信息")
                                                                                 .setMessage("两次密码不一样").setPositiveButton("确定", null).show();
                                                                     }
                                                                 } else {
                                                                     AlertDialog builder = new AlertDialog.Builder(EditPassword.this).setTitle("错误信息")
                                                                             .setMessage("确认密码不能为空").setPositiveButton("确定", null).show();
                                                                 }

                                                             } else {
                                                                 AlertDialog builder = new AlertDialog.Builder(EditPassword.this).setTitle("错误信息")
                                                                         .setMessage("密码为6-20位，由数字和字母组合而成，不能输入空格").setPositiveButton("确定", null).show();
                                                             }
                                                         } else {
                                                             AlertDialog builder = new AlertDialog.Builder(EditPassword.this).setTitle("错误信息")
                                                                     .setMessage("密码为6-20位，由数字和字母组合而成，不能输入空格").setPositiveButton("确定", null).show();
                                                         }
                                                     } else {
                                                         AlertDialog builder = new AlertDialog.Builder(EditPassword.this).setTitle("错误信息")
                                                                 .setMessage("新密码不能为空").setPositiveButton("确定", null).show();
                                                     }
                                                 }
                                             } else {
                                                 if (oldpassword != null && !"".equals(oldpassword)) {
                                                     if (oldpassword.length() >= 6 && oldpassword.length() <= 20) {
                                                         if (oldpassword.matches(Validate.passwordValidate)) {
                                                             if (!"".equals(newpassword) || newpassword != null) {
                                                                 if (newpassword.length() >= 6 && newpassword.length() <= 20) {
                                                                     if (newPassword.getText().toString().matches(Validate.passwordValidate)) {
                                                                         if (!"".equals(repassword) || repassword != null) {
                                                                             if (newpassword.equals(repassword)) {
                                                                                 String userid = userBeans.get(0).getUserid();
                                                                                 Log.d("userid1", userid);
                                                                                 showProgressDialog("正在提交");
                                                                                 new GetEditPassword(userid, newpassword, oldpassword, "1").execute();
                                                                                 index = 2;
                                                                             } else {
                                                                                 AlertDialog builder = new AlertDialog.Builder(EditPassword.this).setTitle("错误信息")
                                                                                         .setMessage("两次密码不一样").setPositiveButton("确定", null).show();
                                                                             }

                                                                         } else {
                                                                             AlertDialog builder = new AlertDialog.Builder(EditPassword.this).setTitle("错误信息")
                                                                                     .setMessage("确认密码不能为空").setPositiveButton("确定", null).show();
                                                                         }

                                                                     } else {
                                                                         AlertDialog builder = new AlertDialog.Builder(EditPassword.this).setTitle("错误信息")
                                                                                 .setMessage("密码为6-20位，由数字和字母组合而成，不能输入空格").setPositiveButton("确定", null).show();
                                                                     }


                                                                 } else {
                                                                     AlertDialog builder = new AlertDialog.Builder(EditPassword.this).setTitle("错误信息")
                                                                             .setMessage("密码为6-20位，由数字和字母组合而成，不能输入空格").setPositiveButton("确定", null).show();
                                                                 }

                                                             } else {
                                                                 AlertDialog builder = new AlertDialog.Builder(EditPassword.this).setTitle("错误信息")
                                                                         .setMessage("新密码不能为空").setPositiveButton("确定", null).show();
                                                             }


                                                         } else {
                                                             AlertDialog builder = new AlertDialog.Builder(EditPassword.this).setTitle("错误信息")
                                                                     .setMessage("密码为6-20位，由数字和字母组合而成，不能输入空格").setPositiveButton("确定", null).show();
                                                         }
                                                     } else {
                                                         AlertDialog builder = new AlertDialog.Builder(EditPassword.this).setTitle("错误信息")
                                                                 .setMessage("密码为6-20位，由数字和字母组合而成，不能输入空格").setPositiveButton("确定", null).show();
                                                     }
                                                 } else {
                                                     AlertDialog builder = new AlertDialog.Builder(EditPassword.this).setTitle("错误信息")
                                                             .setMessage("密码不能为空").setPositiveButton("确定", null).show();
                                                 }

                                             }
                                         }
                                     }
        );


    }

    private void findActionBar() {
        actionBarView = (ActionBarView) findViewById(R.id.edit_password);
        actionBarView.setTitle("修改密码");
        actionBarView.checkBox.setVisibility(View.GONE);
    }

    @Override
    public void update(Observable observable, Object data) {
        DataCenter.flag flag = (DataCenter.flag) data;
        if (!HttpUtils.isConnected()){
            closeProgressDialog();
            return;
        }
        closeProgressDialog();
        if (flag == DataCenter.flag.GET_RE_PASSWORD_SUCCESS
                || flag == DataCenter.flag.GET_RE_PASSWORD_FAILED) { //类表数据

            String code = DataCenter.code;
            if (flag == DataCenter.flag.GET_RE_PASSWORD_SUCCESS) {
                DataCenter.password = newpassword;
                if (code.equals(DataCenter.successFlag)) {
                    Log.d("-=-=-=-=-=-", "-=-=-=-=-");
                    closeProgressDialog();
                    saveUserDetail();
                    if (index == 1) {
                        Log.d("-=-=-=-=-=-", "-=-=-=-=-++++");
                        Intent intent = new Intent(EditPassword.this, LoginOrRegistered.class);
                        intent.putExtra("forget", "forget");
                        startActivity(intent);
                    } else if (index == 2) {
                        Intent intent = new Intent(EditPassword.this, LoginOrRegistered.class);
                        startActivity(intent);
                    }
                    Toast.makeText(EditPassword.this, DataCenter.msg, Toast.LENGTH_LONG).show();
                } else {
                    closeProgressDialog();
                    Toast.makeText(EditPassword.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                    return;
                }
            } else {
                closeProgressDialog();
                Toast.makeText(EditPassword.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                return;
            }

            if (flag == DataCenter.flag.GET_RE_PASSWORD_FAILED) {
                closeProgressDialog();
               // Toast.makeText(EditPassword.this, "failed", Toast.LENGTH_SHORT).show();
                return;
            }
        }
    }

    public void saveUserDetail() {

        sharedPreferences = getSharedPreferences("user_detail", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("password", DataCenter.password);
        editor.commit();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d(DataCenter.LOG_INFO, EditPassword.class.getSimpleName() + ".onDestroy");
        //删除观察者
        DataCenter.getInstance().deleteObserver(this);
    }

}
